package com.billsharing.server;

import java.sql.Connection;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

public class Bill {

    static int bid;
    String description;
    int noOfParticipants;
    ArrayList<User> users = new ArrayList<User>();
    double totalAmount;
    User payer;
    double individualAmount;
    Group group;
    Date billDate;

    Bill() {

    	
    }

    Bill(int billId) {
    	DbConnect dbconn = new DbConnect();
    	Connection conn = dbconn.getConnection();
    	try
    	{
    	Statement s = conn.createStatement();
    	
    	ResultSet rs= s.executeQuery("select * from Bill where billid = "+billId+"");
    		if (rs != null && rs.next()) {
    		    bid = rs.getInt("billid");
    		    description = rs.getString("billdes");
    		    totalAmount = rs.getDouble("billamt");
    		    User user = new User(rs.getInt("billid"));
    		    payer = user;
    		    billDate = rs.getDate("date");
    		}
    	}
    	
    	
    	catch(Exception e)
    	{
    		e.printStackTrace();
    	}
    }
    
    
    
    Bill(double amount,String description, User payer,User sharedBetween,Date date )
    {
    	DbConnect dbconn = new DbConnect();
    	Connection conn = dbconn.getConnection();
    	try
    	{
    	Statement s = conn.createStatement();
    	int i =s.executeUpdate("insert into Bill (billamt,billdesc, paidid, sharedid,date) values ("+amount+",'"+description+"',"+payer+","+sharedBetween+",\""+date+"\")",s.RETURN_GENERATED_KEYS);
    	if(i > 0)
    	{
    		ResultSet rs = s.getGeneratedKeys();
    		if (rs != null && rs.next()) {
    		    bid = rs.getInt(1);
    		}
    	}
    	
    	}
    	catch(Exception e)
    	{
    		e.printStackTrace();
    	}
    }
    
    
    
    /**
     * 
     * 
     * 
     * @param usersList
     * @param billAmount
     */

    Bill(ArrayList<User> usersList, double billAmount) {
        totalAmount = billAmount;
        users = usersList;

    }
    
    Bill(Group billGroup,double billAmount,User billPayer)
    {
        group = billGroup;
        totalAmount = billAmount;
        payer = billPayer;
        
    }

    /*
     * This function takes in the list of users that participated in the bill
     */
    void addUserList(ArrayList<Integer> users) {

    }

    void addUserToBill(int userId) {

    }

    void setTotalAmount(double billAmount) {
        totalAmount = billAmount;

    }

    double getTotalAmount() {
        return totalAmount;
    }
    
    double getIndividualAmount()
    {
        noOfParticipants = users.size();
        individualAmount = totalAmount/noOfParticipants;
        return individualAmount;
    }
    
    String GrouporFriend(String shared)
    {
    	String result = null;
    	try
    	{
    	DbConnect dbconn = new DbConnect();
		Connection conn;
		conn = dbconn.getConnection();
		Statement s= conn.createStatement();
		Statement s1= conn.createStatement();
		s.executeQuery("select * from Friend where fndemail='"+shared+"'");
		ResultSet rs=s.getResultSet();
		s1.executeQuery("select * from Groups where groupname='"+shared+"'");
		ResultSet rs1=s1.getResultSet();
			if(rs.next())
			{
				result="fnd";
			}
			else if(rs1.next())
			{
				result="grp";
			}
			
    	}
    	catch(Exception e)
    	{
    	  e.printStackTrace();
    	}
		return result;
    }
    
  boolean reportBilltoFriend(String billdesc,Double billamt,String paidemail,String sharedemail)
    {
	  boolean result = false;
	  String credit1=null, debit1=null;
	  double credit=0,debit=0;
	  try {
			DbConnect dbconn = new DbConnect();
			Connection conn;
			conn = dbconn.getConnection();
			Statement s=conn.createStatement();
			Statement s1=conn.createStatement();
            Statement s2=conn.createStatement();
	  User fndid=new User(paidemail);
	  int paidid1=fndid.getUid();
	  User sharedid=new User(sharedemail);
	  int sharedid1=sharedid.getUid();
	  int i3 =s.executeUpdate("insert into Bill (billamt,billdesc,paidid,sharedid) values ("+billamt+",'"+billdesc+"',"+paidid1+","+sharedid1+")");
	  int i1 = 0, i2=0, i=0;
      if(i3>0)
      {
      	s2.executeQuery("select credit,debit from Friend where userid="+paidid1);
      	ResultSet rs=s2.getResultSet();
      	while(rs.next())
      	{
      		
      	credit1=rs.getString("credit");
      	debit1= rs.getString("debit");
      	credit= Double.parseDouble(credit1);
      	debit= Double.parseDouble(debit1);
      	System.out.println("Credit is"+ credit);
      	System.out.println("Debit is"+ debit);
      	}
      	double billshared=billamt/2;
      	System.out.println("billshared "+billshared);
      	if(credit==0 && debit==0)
      	{
      		System.out.println("Inside intial loop");
      		i1 =s.executeUpdate("UPDATE Friend SET credit="+billshared+" where userid="+paidid1+" and fndemail='"+sharedemail+"'");
      	 
      		i2 =s1.executeUpdate("UPDATE Friend SET debit="+billshared+" where userid="+sharedid1+" and fndemail='"+paidemail+"'");
      		 
      	}
      	else if(credit>=debit)
      	
      	{
      		System.out.println("Inside credit>debit loop");
      		billshared=billshared+credit;
      		System.out.println("billshared "+billshared);
      		i1 =s.executeUpdate("UPDATE Friend SET credit="+billshared+" where userid="+paidid1+" and fndemail='"+sharedemail+"'");
      		 
      		i2 =s1.executeUpdate("UPDATE Friend SET debit="+billshared+" where userid="+sharedid1+" and fndemail='"+paidemail+"'");
      		 
      	}
      	
      	else if(debit>credit)
      	{ 
      		System.out.println("Inside debit>credit loop");
      		 if (debit>=billshared)
      		 {
      			 System.out.println("Inside debit>billshared loop");
      		billshared=debit-billshared;
      		i1 =s.executeUpdate("UPDATE Friend SET credit="+i+",debit="+billshared+" where userid="+paidid1+" and fndemail='"+sharedemail+"'");
      		 
      		i2 =s1.executeUpdate("UPDATE Friend SET credit="+billshared+",debit="+i+" where userid="+sharedid1+" and fndemail='"+paidemail+"'");
      		// 
      		 }
      		 else
      		 {
      			 System.out.println("Inside final loop");
      			billshared=billshared-debit;
      			i1 =s.executeUpdate("UPDATE Friend SET credit="+billshared+",debit="+i+" where userid="+paidid1+" and fndemail='"+sharedemail+"'");
      			
      			i2 =s1.executeUpdate("UPDATE Friend SET credit="+i+",debit="+billshared+" where userid="+sharedid1+" and fndemail='"+paidemail+"'");
      		
      		 }
      		 
      		 System.out.println(" Credit is"+ credit);
           	System.out.println(" Debit is"+ debit);
           	System.out.println("billshared"+billshared);
      	}
      	if(i1>0)
    	{
    		result=true;
    	}
    	else
    	{
    		result=false;
    	}
    	
    }
    return result;
	  }
	  catch(Exception e)
	  {
		  e.printStackTrace();
	  }
	  return result;
    }
  
  
  
  
  
  boolean reportBilltoGroup(String billdesc,Double billamt,String paidemail,int grpid, int count)
  {
	  boolean result = false;
	  String credit1=null, debit1=null;
	  double credit=0,debit=0;
	  try {
			DbConnect dbconn = new DbConnect();
			Connection conn;
			conn = dbconn.getConnection();
			Statement s=conn.createStatement();
			Statement s1=conn.createStatement();
			Statement s2=conn.createStatement();
			Statement s3=conn.createStatement();
			System.out.println("Billamt shared in grp:"+billamt);
			System.out.println(grpid);
			s3.executeQuery("select groupuserid from GroupMembers where groupid="+grpid);
			ResultSet rs=s3.getResultSet();
			billamt=billamt/count;
			while(rs.next())
			{
				int sharedid=rs.getInt("groupuserid");
				User user=new User(sharedid);
				String sharedemail=user.getEmail();
				if(sharedemail==paidemail)
				{
					result=true;
				}
				else
				{
				
					User fndid=new User(paidemail);
					  int paidid1=fndid.getUid();
					  User sharedid2=new User(sharedemail);
					  int sharedid1=sharedid2.getUid();
					  int i3 =s.executeUpdate("insert into Bill (billamt,billdesc,paidid,sharedid) values ("+billamt+",'"+billdesc+"',"+paidid1+","+sharedid1+")");
					  
					  int i1 = 0, i2=0, i=0;
				      if(i3>0)
				      {
				      	s2.executeQuery("select credit,debit from Friend where userid="+paidid1);
				      	ResultSet rs1=s2.getResultSet();
					      	while(rs1.next())
					      	{
					      		
					      	credit1=rs1.getString("credit");
					      	debit1= rs1.getString("debit");
					      	credit= Double.parseDouble(credit1);
					      	debit= Double.parseDouble(debit1);
					      	System.out.println("Credit is"+ credit);
					      	System.out.println("Debit is"+ debit);
					      	}
					      	double billshared=billamt;
					      	System.out.println("billshared "+billshared);
					      	if(credit==0 && debit==0)
					      	{
					      		System.out.println("Inside intial loop");
					      		i1 =s.executeUpdate("UPDATE Friend SET credit="+billshared+" where userid="+paidid1+" and fndemail='"+sharedemail+"'");
					      	 
					      		i2 =s1.executeUpdate("UPDATE Friend SET debit="+billshared+" where userid="+sharedid1+" and fndemail='"+paidemail+"'");
					      		 
					      	}
					      	else if(credit>=debit)
					      	
					      	{
					      		System.out.println("Inside credit>debit loop");
					      		billshared=billshared+credit;
					      		System.out.println("billshared "+billshared);
					      		i1 =s.executeUpdate("UPDATE Friend SET credit="+billshared+" where userid="+paidid1+" and fndemail='"+sharedemail+"'");
					      		 
					      		i2 =s1.executeUpdate("UPDATE Friend SET debit="+billshared+" where userid="+sharedid1+" and fndemail='"+paidemail+"'");
					      		 
					      	}
					      	
					      	else if(debit>credit)
					      	{ 
					      		System.out.println("Inside debit>credit loop");
					      		 if (debit>=billshared)
					      		 {
					      			 System.out.println("Inside debit>billshared loop");
					      		billshared=debit-billshared;
					      		i1 =s.executeUpdate("UPDATE Friend SET credit="+i+",debit="+billshared+" where userid="+paidid1+" and fndemail='"+sharedemail+"'");
					      		 
					      		i2 =s1.executeUpdate("UPDATE Friend SET credit="+billshared+",debit="+i+" where userid="+sharedid1+" and fndemail='"+paidemail+"'");
					      		// 
					      		 }
					      		 else
					      		 {
					      			 System.out.println("Inside final loop");
					      			billshared=billshared-debit;
					      			i1 =s.executeUpdate("UPDATE Friend SET credit="+billshared+",debit="+i+" where userid="+paidid1+" and fndemail='"+sharedemail+"'");
					      			
					      			i2 =s1.executeUpdate("UPDATE Friend SET credit="+i+",debit="+billshared+" where userid="+sharedid1+" and fndemail='"+paidemail+"'");
					      		
					      		 }
					      		 
					      		 System.out.println(" Credit is"+ credit);
					           	System.out.println(" Debit is"+ debit);
					           	System.out.println("billshared"+billshared);
					           	
					           	if(i1>0 && i2>0)
					           	{
					           		result=true;
					           	}
					           	
				      	}
				      	
					      					
								
				}
				
				}
			}
	  	}
	  catch(Exception e)
	  {
		  e.printStackTrace();
	  }
	return result;
}
}